/**
 * Created by xps-13 on 2017/1/5.
 */
import java.sql.*;
//连接数据库
public class Database {
    Connection conn;
    public void getConnection()  {
        try
        {
            String url="jdbc:mysql://localhost:3306/homework5";
            String user="root";
            String password="123456";
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url,user,password);
            if(!conn.isClosed()) System.out.println("成功连接！");
        }
        catch (Exception ex)
        {
            System.out.println("Error : " + ex.toString());
        }
    }

    //向数据库插入信息
    public boolean setInformation(Information information) {
        try{
            String insql="insert into video(avnumber,creator,coins,collections,category)values(?,?,?,?,?)";
            PreparedStatement ps=conn.prepareStatement(insql);
            ps.setString(1, information.getAvNumber());
            ps.setString(2,information.getCreator());
            ps.setInt(3, information.getCoins());
            ps.setInt(4, information.getCollections());
            ps.setString(5, information.getCategory());                      //将信息插入表中
            int result=ps.executeUpdate();
            if(result>0)
                return true;            //判断是否插入成功
        }catch(Exception e){
            e.printStackTrace();
        }
        return false;
    }
}
